#include "adc.h"
uint8_t adc_ReadTime1, adc_ReadTime2, adc_ReadTime3, adc_ReadTime4; // ms
uint32_t adc_value[4];

void adc_dma_config()
{
	/* ADC_DMA_channel configuration */
	dma_parameter_struct dma_data_parameter;

	/* ADC_DMA_channel deinit */
	dma_deinit(DMA0, DMA_CH0);
	rcu_periph_clock_enable(RCU_DMA0);
	/* initialize DMA single data mode */
	dma_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
	dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dma_data_parameter.memory_addr = (uint32_t)(adc_value);
	dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_32BIT;
	dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_32BIT;
	dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
	dma_data_parameter.number = 4;
	dma_data_parameter.priority = DMA_PRIORITY_HIGH;
	dma_init(DMA0, DMA_CH0, &dma_data_parameter);

	dma_circulation_enable(DMA0, DMA_CH0);

	/* enable DMA channel */
	dma_channel_enable(DMA0, DMA_CH0);
}


void adc_config()
{
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_ADC0);
	adc_deinit(ADC0);
	rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV12);
	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_1| GPIO_PIN_3); //GPIO_PIN_2 
	/* ADC scan mode function enable */
	adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
	adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
	/* ADC trigger config */
	adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
	/* ADC data alignment config */
	adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
	/* configure the ADC sync mode */
	//	 adc_mode_config(ADC_DAUL_REGULAL_PARALLEL);
	/* ADC channel length config */
	adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 4);
	/* ADC regular channel config */
	adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_239POINT5);
	adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_11, ADC_SAMPLETIME_239POINT5);
	// adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_12, ADC_SAMPLETIME_239POINT5);
	adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_13, ADC_SAMPLETIME_239POINT5);
	/* ADC external trigger enable */
	adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
	/* enable ADC interface */
	adc_enable(ADC0);
	delay_1ms(1);
	/* ADC calibration and reset calibration */
	adc_calibration_enable(ADC0);
	/* ADC DMA function enable */
	adc_dma_mode_enable(ADC0);

	adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);

}

int16_t ADC_humidity() //测完9次后返回adc值
{
	static uint16_t adctemp[9];
	static uint8_t n = 0;
	uint8_t i, j;
	uint16_t temp;
	if (0 == adc_ReadTime4)
	{
		adctemp[n] = adc_value[3];
		n++;
		adc_ReadTime4 = 50; // 50ms采样一次值
	}
	if (n >= 9) //取中间值
	{
		for (i = 0; i < 8; i++)
		{
			for (j = 0; j < 8; j++)
			{
				if (adctemp[j] > adctemp[j + 1])
				{
					temp = adctemp[j];
					adctemp[j] = adctemp[j + 1];
					adctemp[j + 1] = temp;
				}
			}
		}
		n = 0;
		return adctemp[4];
	}
	else
	{
		return -1;
	}
}

uint16_t ADC_PowerCheck() //测完9次后返回adc值
{
	static uint16_t adctemp[9];
	static uint8_t n = 0;
	uint8_t i, j;
	uint16_t temp;
	if (0 == adc_ReadTime3)
	{
		adctemp[n] = adc_value[2];
		n++;
		adc_ReadTime3 = 1; // 1ms测一次
	}
	if (n >= 9)
	{
		for (i = 0; i < 8; i++)
		{
			for (j = 0; j < 8; j++)
			{
				if (adctemp[j] > adctemp[j + 1])
				{
					temp = adctemp[j];
					adctemp[j] = adctemp[j + 1];
					adctemp[j + 1] = temp;
				}
			}
		}
		n = 0;
		return adctemp[4];
	}
	else
		return 0;
}

uint16_t ADC_AirPress1() //测完9次后返回adc值
{
	static uint16_t adctemp[9];
	static uint8_t n = 0;
	uint8_t i, j;
	uint16_t temp;
	if (0 == adc_ReadTime1)
	{
		adctemp[n] = adc_value[0];
		n++;
		adc_ReadTime1 = 50;  //每50ms采集一次数据
	}
	if (n >= 9)
	{
		for (i = 0; i < 8; i++)
		{
			for (j = 0; j < 8; j++)
			{
				if (adctemp[j] > adctemp[j + 1])
				{
					temp = adctemp[j];
					adctemp[j] = adctemp[j + 1];
					adctemp[j + 1] = temp;
				}
			}
		}
		n = 0;
		return adctemp[4];
	}
	else
		return 0;
}

uint16_t ADC_AirPress2() //测完9次后返回adc值
{
	static uint16_t adctemp[9];
	static uint8_t n = 0;
	uint8_t i, j;
	uint16_t temp;
	if (0 == adc_ReadTime2)
	{
		adctemp[n] = adc_value[1];
		n++;
		adc_ReadTime2 = 50;
	}
	if (n >= 9)
	{
		for (i = 0; i < 8; i++)
		{
			for (j = 0; j < 8; j++)
			{
				if (adctemp[j] > adctemp[j + 1])
				{
					temp = adctemp[j];
					adctemp[j] = adctemp[j + 1];
					adctemp[j + 1] = temp;
				}
			}
		}
		n = 0;
		return adctemp[4];
	}
	else
		return 0;
}
